package pe.gob.osinergmin.interfase_siaf.dac.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import pe.gob.osinergmin.interfase_siaf.dac.dao.FaseDao;
import pe.gob.osinergmin.interfase_siaf.dac.domain.Fase;

@Repository("faseDao")
public class FaseDaoImpl implements FaseDao {

    private final Log logger = LogFactory.getLog(getClass());
	
	
	private NamedParameterJdbcTemplate jdbcParameterTemplateData;	
	private JdbcTemplate jdbcTemplateData;	
	
	
	
	@Autowired
	public void SetDataSource(DataSource dataSourceData){
		this.jdbcTemplateData = new JdbcTemplate(dataSourceData);
		this.jdbcParameterTemplateData = new NamedParameterJdbcTemplate(dataSourceData);
		
	}
	
	
	/**DECLARACION DE SQL**/
	private static final String SQL_SELECT_FASE ="SELECT * FROM fase ";
	private static final String SQL_SELECT_FASE_PARAMETER ="SELECT * FROM fase WHERE fase =:fase";
	private static final String SQL_SELECT_FASE_PARAMETER1 ="SELECT * FROM fase WHERE ciclo =:ciclo";
	private static final String SQL_SELECT_FASE_PARAMETER2 ="SELECT * FROM fase WHERE fase =:fase AND ciclo =:ciclo";

	private static final String SQL_SELECT_FASE_PARAMETER3 ="SELECT COUNT(fase) FROM fase WHERE fase =:fase";

	@Override
	public List<Fase> listarFases() {
		logger.info("Ingresando a listar fases"); 
		RowMapper<Fase> lista = ParameterizedBeanPropertyRowMapper.newInstance(Fase.class);
		return this.jdbcTemplateData.query(SQL_SELECT_FASE, lista); 
	}
	@Override
	public List<Fase> listarFasesParameter(Fase fase) {		
		logger.info("Ingresando a listar fases parameters"); 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(fase);
		RowMapper<Fase> lista = ParameterizedBeanPropertyRowMapper.newInstance(Fase.class);	
		if(StringUtils.isNoneBlank(fase.getFase()) && StringUtils.isNoneBlank(fase.getCiclo())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER2, parameters, lista);	
		}if(StringUtils.isNoneBlank(fase.getFase())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER, parameters, lista);	
		}else if(StringUtils.isNoneBlank(fase.getCiclo())){
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER1, parameters, lista);	
		}else{
			return null;
		}			
	}
	
	/**IMPLEMENTACION DE METODOS*/	
	@Override
	public int verificarFase(Fase f) throws Exception {		 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(f);			
	    return this.jdbcParameterTemplateData.queryForInt(SQL_SELECT_FASE_PARAMETER3, parameters);	
	}
	
}
